package ModelLayer;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Controller class for the address book
 * 
 * @author (Group 1) 
 * @version (2013.11.04)
 */
public class AddressBook
{
    private HashMap<Long,Person> persons;
    private static AddressBook instance = null;
    
    private AddressBook() {  persons = new HashMap<>();}
    public static AddressBook getInstance() { if(instance==null) instance = new AddressBook();  return instance; }
    
    
    // return a Person object
    public Person getPerson(long id)
    {
        if(persons.containsKey(id)) return persons.get(id);
        return null;
    }
    
    public void createPerson(long id, String name, String address, String postalCode, String city, String phone)
    {   
        Person person1 = new Person(id, name, address, postalCode, city, phone);
        persons.put((Long)id,person1);

    }
    

    public boolean deletePerson(long id)
    {
        if(persons.containsKey(id) == false) return false;
        persons.remove(id);
        return true;
    }

    public boolean updatePerson(long id,int choice, String updateValue)
    {
        Person person = persons.get(id);
        if(person==null || updateValue==null) return false;
        
        switch(choice)
        {
            case 1 :
            {
                person.setName(updateValue);
            }break;
            
            case 2 :
            {
                person.setAddress(updateValue);
            }break;
            
            case 3 :
            {
                person.setPostalCode(updateValue);
            }break;
            
            case 4 :
            {
                person.setCity(updateValue);
            }break;
            
            case 5 :
            {
                person.setPhone(updateValue);
            }break;
            
            default: return false;
        }
        
        return true;
    }

    public Iterator listAllPersons()
    {
        Iterator it = persons.values().iterator();
        return it;
    }
}
